﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Core;
using IPC.Communication.Core.Net;
using IPC.Communication.Reflection;

namespace IPC.Communication.Profinet.Freedom
{
    public class FreedomTcpNet : NetworkDeviceBase
    {
        public FreedomTcpNet()
        {
            base.ByteTransform = new RegularByteTransform();
        }

        public FreedomTcpNet(string ipAddress, int port)
        {
            IpAddress = ipAddress;
            Port = port;
            base.ByteTransform = new RegularByteTransform();
        }

        [HslMqttApi("ReadByteArray", "特殊的地址格式，需要采用解析包起始地址的报文，例如 modbus 协议为 stx=9;00 00 00 00 00 06 01 03 00 64 00 01")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            OperateResult<byte[], int> operateResult = AnalysisAddress(address);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult);
            }
            OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content1);
            if (!operateResult2.IsSuccess)
            {
                return operateResult2;
            }
            if (operateResult.Content2 >= operateResult2.Content.Length)
            {
                return new OperateResult<byte[]>(StringResources.Language.ReceiveDataLengthTooShort);
            }
            return OperateResult.CreateSuccessResult(operateResult2.Content.RemoveBegin(operateResult.Content2));
        }

        public override OperateResult Write(string address, byte[] value)
        {
            return Read(address, 0);
        }

        //[AsyncStateMachine(typeof(<ReadAsync>d__4))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__4 stateMachine = new <ReadAsync>d__4();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__5))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__5 stateMachine = new <WriteAsync>d__5();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("FreedomTcpNet<{0}>[{1}:{2}]", base.ByteTransform.GetType(), IpAddress, Port);
        }

        public static OperateResult<byte[], int> AnalysisAddress(string address)
        {
            try
            {
                int value = 0;
                byte[] value2 = null;
                if (address.IndexOf(';') > 0)
                {
                    string[] array = address.Split(new char[1]
				{
					';'
				}, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i].StartsWith("stx="))
                        {
                            value = Convert.ToInt32(array[i].Substring(4));
                        }
                        else
                        {
                            value2 = array[i].ToHexBytes();
                        }
                    }
                }
                else
                {
                    value2 = address.ToHexBytes();
                }
                return OperateResult.CreateSuccessResult(value2, value);
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[], int>(ex.Message);
            }
        }
    }
}